﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml;

using IServiceProvider = System.IServiceProvider;
using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace mdv.wssSchemaExplorer
{
    /// <summary>
    /// Summary description for MyControl.
    /// </summary>
    public partial class MyControl : UserControl
    {
        private MyToolWindow pane;
        public MyControl(MyToolWindow myToolWindowPane)
        {
            InitializeComponent();
            pane = myToolWindowPane;
        }
        /// <summary> 
        /// Let this control process the mnemonics.
        /// </summary>
        protected override bool ProcessDialogChar(char charCode)
        {
              // If we're the top-level form or control, we need to do the mnemonic handling
              if (charCode != ' ' && ProcessMnemonic(charCode))
              {
                    return true;
              }
              return base.ProcessDialogChar(charCode);
        }
        private void MyControl_Click(object sender, EventArgs e)
        {
            tvSchema.Select();
        }
        public void LoadSchema(string schemaPath)
        {
            tvSchema.Nodes.Clear();
            XmlDocument doc = new XmlDocument();
            doc.Load(schemaPath);
            XmlElement xdList = doc.DocumentElement;
            ProcessListNode(xdList);
            tvSchema.ExpandAll();
        }
        protected void ProcessListNode(XmlElement listNode)
        {
            try
            {
                WSS.wssListController lc = new WSS.wssListController(new WSS.wssList(listNode));
                TreeNode tn = new TreeNode(lc.ToString());
                tn.Tag = lc;
                tvSchema.Nodes.Add(tn);
                ConstructTree(tn, lc);
            }
            catch (Exception ex)
            {
                tvSchema.Nodes.Add(ex.Message);
            }
        }
        protected void ConstructTree(TreeNode rootNode, WSS.IController rootObject)
        {
            foreach (WSS.IController to in rootObject.Children)
            {
                TreeNode tn = new TreeNode(to.ToString());
                tn.Tag = to;
                rootNode.Nodes.Add(tn);
                if (null != to.Children) ConstructTree(tn, to);
            }
        }
        private void tvSchema_MouseClick(object sender, MouseEventArgs e)
        {
            TreeView tv = (TreeView) sender;
	        Point p = new Point(e.X, e.Y);
            TreeNode node = tv.GetNodeAt(p);
            WSS.IController selectedObject = node.Tag as WSS.IController;
            if (null != selectedObject)
            {
                pane.SelectList(new object[1] { selectedObject.Model });
                pane.UpdateSelection();
                if (node != null)
                {
                    ((TreeView)sender).SelectedNode = node;
                    if ((e.Button == MouseButtons.Right) && (null != selectedObject.Actions))
                    {
                        selectedObject.Actions.Show(tv, p);
                    }
                }
            }
        }
    }
}